package com.ywj.demo02_concurrent_problem;

import org.openjdk.jcstress.annotations.*;
import org.openjdk.jcstress.infra.results.I_Result;

@JCStressTest     //用并发压缩工具jcstress 测试类方法
//@Outcome(id = {"1", "4"}, expect = Expect.ACCEPTABLE, desc = "ok")    //@Outcome 对输出结果进行处理， 1 4 打印ok
//@Outcome(id = "0", expect = Expect.ACCEPTABLE_INTERESTING, desc = "danger")
@Outcome(id = {"1"}, expect = Expect.ACCEPTABLE, desc = "ok")    //@Outcome 对输出结果进行处理， 1 4 打印ok
@Outcome(id = {"4"}, expect = Expect.ACCEPTABLE_INTERESTING, desc = "danger1")
@Outcome(id = "0", expect = Expect.ACCEPTABLE_INTERESTING, desc = "danger")
@State
public class TestOrdering {
//    private static Object obj = new Object();
//    int num = 0;
//    boolean ready = false;
//
//    //线程1执行的代码
//    @Actor   //@Actor 表示有多个线程来操作这两个方法
//    public void actor1(I_Result r) {
//        synchronized (obj) {
//            if (ready) {
//                r.r1 = num + num;
//            } else {
//                r.r1 = 1;
//            }
//        }
//    }
//
//    //线程2执行的代码  对两个变量进行修改
//    @Actor
//    public void actor2(I_Result r) {
//        synchronized (obj) {
//            num = 2;
//            ready = true;
//        }
//    }

    volatile int num = 0;
    volatile boolean ready = false;

    @Actor   //@Actor 表示有多个线程来操作这两个方法
    public void actor1(I_Result r) {
        if (ready) {
            r.r1 = num + num;
        } else {
            r.r1 = 1;
        }
    }

    @Actor
    public void actor2(I_Result r) {
        num = 2;
        ready = true;
    }
}
